package com.java.base.exercise.current;

import java.util.HashSet;
import java.util.concurrent.TimeUnit;

/**
 * @Author RenPu
 * @Date 2021/9/3 14:39
 * @Version 1.0
 * @Description: 创建了10个线程，编号0~9，每个线程调用前一个线程的
 * join()方法，也就是线程0结束了，线程1才能从join()方法中返回，而线程0需要等待main线程结
 * 束。
 **/
public class Join {
    public static void main(String[] args) throws InterruptedException {

        HashSet<Object> objects = new HashSet<>();
        objects.add("aaa");


        Thread previous = Thread.currentThread();

        for (int i = 0; i < 10; i++) {
// 每个线程拥有前一个线程的引用，需要等待前一个线程终止，才能从等待中返回
            Thread thread = new Thread(new Domino(previous), String.valueOf(i));
            thread.start();
            previous = thread;
        }
        TimeUnit.SECONDS.sleep(5);
        System.out.println(Thread.currentThread().getName() + " terminate.");
    }


    static class Domino implements Runnable {
        private Thread thread;

        public Domino(Thread thread) {
            this.thread = thread;
        }

        @Override
        public void run() {
            try {
                thread.join();
            } catch (InterruptedException e) {
            }
            System.out.println(Thread.currentThread().getName() + " terminate.");
        }
    }

}




